package cetcbigdata.da.personal_service.beijing;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;


/**
 * @author YuanGongPing
 * @version 0.1
 * @description Js
 * @since 2022/8/3 17:28
 */


public class Js {
    public static String jsCode = "\n" +
            "\n" +
            "var base64EncodeChars = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\";\n" +
            "\n" +
            "var base64DecodeChars = new Array(\n" +
            "\n" +
            "    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n" +
            "\n" +
            "    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,\n" +
            "\n" +
            "    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,\n" +
            "\n" +
            "    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,\n" +
            "\n" +
            "    -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,\n" +
            "\n" +
            "    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,\n" +
            "\n" +
            "    -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,\n" +
            "\n" +
            "    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);\n" +
            "\n" +
            "//编码的方法\n" +
            "\n" +
            "function base64encode(str) {\n" +
            "\n" +
            "    var out, i, len;\n" +
            "\n" +
            "    var c1, c2, c3;\n" +
            "\n" +
            "    len = str.length;\n" +
            "\n" +
            "    i = 0;\n" +
            "\n" +
            "    out = \"\";\n" +
            "\n" +
            "    while(i < len) {\n" +
            "\n" +
            "        c1 = str.charCodeAt(i++) & 0xff;\n" +
            "\n" +
            "        if(i == len)\n" +
            "\n" +
            "        {\n" +
            "\n" +
            "            out += base64EncodeChars.charAt(c1 >> 2);\n" +
            "\n" +
            "            out += base64EncodeChars.charAt((c1 & 0x3) << 4);\n" +
            "\n" +
            "            out += \"==\";\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "        c2 = str.charCodeAt(i++);\n" +
            "\n" +
            "        if(i == len)\n" +
            "\n" +
            "        {\n" +
            "\n" +
            "            out += base64EncodeChars.charAt(c1 >> 2);\n" +
            "\n" +
            "            out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));\n" +
            "\n" +
            "            out += base64EncodeChars.charAt((c2 & 0xF) << 2);\n" +
            "\n" +
            "            out += \"=\";\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "        c3 = str.charCodeAt(i++);\n" +
            "\n" +
            "        out += base64EncodeChars.charAt(c1 >> 2);\n" +
            "\n" +
            "        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));\n" +
            "\n" +
            "        out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));\n" +
            "\n" +
            "        out += base64EncodeChars.charAt(c3 & 0x3F);\n" +
            "\n" +
            "    }\n" +
            "\n" +
            "    return out;\n" +
            "\n" +
            "}\n" +
            "\n" +
            "//解码的方法\n" +
            "\n" +
            "function base64decode(str) {\n" +
            "\n" +
            "    var c1, c2, c3, c4;\n" +
            "\n" +
            "    var i, len, out;\n" +
            "\n" +
            "    len = str.length;\n" +
            "\n" +
            "    i = 0;\n" +
            "\n" +
            "    out = \"\";\n" +
            "\n" +
            "    while(i < len) {\n" +
            "\n" +
            "\n" +
            "\n" +
            "        do {\n" +
            "\n" +
            "            c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];\n" +
            "\n" +
            "        } while(i < len && c1 == -1);\n" +
            "\n" +
            "        if(c1 == -1)\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "\n" +
            "\n" +
            "        do {\n" +
            "\n" +
            "            c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];\n" +
            "\n" +
            "        } while(i < len && c2 == -1);\n" +
            "\n" +
            "        if(c2 == -1)\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "        out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));\n" +
            "\n" +
            "\n" +
            "\n" +
            "        do {\n" +
            "\n" +
            "            c3 = str.charCodeAt(i++) & 0xff;\n" +
            "\n" +
            "            if(c3 == 61)\n" +
            "\n" +
            "                return out;\n" +
            "\n" +
            "            c3 = base64DecodeChars[c3];\n" +
            "\n" +
            "        } while(i < len && c3 == -1);\n" +
            "\n" +
            "        if(c3 == -1)\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "        out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));\n" +
            "\n" +
            "\n" +
            "\n" +
            "        do {\n" +
            "\n" +
            "            c4 = str.charCodeAt(i++) & 0xff;\n" +
            "\n" +
            "            if(c4 == 61)\n" +
            "\n" +
            "                return out;\n" +
            "\n" +
            "            c4 = base64DecodeChars[c4];\n" +
            "\n" +
            "        } while(i < len && c4 == -1);\n" +
            "\n" +
            "        if(c4 == -1)\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "        out += String.fromCharCode(((c3 & 0x03) << 6) | c4);\n" +
            "\n" +
            "    }\n" +
            "\n" +
            "    return out;\n" +
            "\n" +
            "}\n" +
            "\n" +
            "function utf16to8(str) {\n" +
            "\n" +
            "    var out, i, len, c;\n" +
            "\n" +
            "    out = \"\";\n" +
            "\n" +
            "    len = str.length;\n" +
            "\n" +
            "    for(i = 0; i < len; i++) {\n" +
            "\n" +
            "        c = str.charCodeAt(i);\n" +
            "\n" +
            "        if ((c >= 0x0001) && (c <= 0x007F)) {\n" +
            "\n" +
            "            out += str.charAt(i);\n" +
            "\n" +
            "        } else if (c > 0x07FF) {\n" +
            "\n" +
            "            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));\n" +
            "\n" +
            "            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));\n" +
            "\n" +
            "            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));\n" +
            "        } else {\n" +
            "\n" +
            "            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));\n" +
            "\n" +
            "            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "    }\n" +
            "\n" +
            "    return out;\n" +
            "\n" +
            "}\n" +
            "\n" +
            "function utf8to16(str) {\n" +
            "\n" +
            "    var out, i, len, c;\n" +
            "\n" +
            "    var char2, char3;\n" +
            "\n" +
            "    out = \"\";\n" +
            "\n" +
            "    len = str.length;\n" +
            "\n" +
            "    i = 0;\n" +
            "\n" +
            "    while(i < len) {\n" +
            "\n" +
            "        c = str.charCodeAt(i++);\n" +
            "\n" +
            "        switch(c >> 4)\n" +
            "\n" +
            "        {\n" +
            "\n" +
            "            case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:\n" +
            "\n" +
            "            // 0xxxxxxx\n" +
            "\n" +
            "            out += str.charAt(i-1);\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "            case 12: case 13:\n" +
            "\n" +
            "            // 110x xxxx 10xx xxxx\n" +
            "\n" +
            "            char2 = str.charCodeAt(i++);\n" +
            "\n" +
            "            out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));\n" +
            "\n" +
            "            break;\n" +
            "\n" +
            "            case 14:\n" +
            "\n" +
            "                // 1110 xxxx 10xx xxxx 10xx xxxx\n" +
            "\n" +
            "                char2 = str.charCodeAt(i++);\n" +
            "\n" +
            "                char3 = str.charCodeAt(i++);\n" +
            "\n" +
            "                out += String.fromCharCode(((c & 0x0F) << 12) |\n" +
            "\n" +
            "                    ((char2 & 0x3F) << 6) |\n" +
            "\n" +
            "                    ((char3 & 0x3F) << 0));\n" +
            "\n" +
            "                break;\n" +
            "\n" +
            "        }\n" +
            "\n" +
            "    }\n" +
            "\n" +
            "    return out;\n" +
            "\n" +
            "}";
    public static String getEncode(String code){
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("js");
        //编写js方法体
        try {
            engine.eval(jsCode);
            //执行js脚本。
            Invocable invocable = (Invocable) engine;
            Object object = invocable.invokeFunction("utf16to8", code);
            object = invocable.invokeFunction("base64encode", object.toString());
            return object.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

}
